---
title: "ROC-India-Control"
author: "AP"
date: "`r Sys.Date()`"
output: html_document
---

```{r}
library(tidyverse)
library(effsize)
library(BayesFactor)
library(dplyr)
```
#Arranging the dataset
```{r}
crmisinfo<- read.csv("long_data.csv")
colnames(crmisinfo)

```

#Discernment Analysis ROC-Reliability
```{r}
control = crmisinfo[crmisinfo$condition == "control",]
preControl= control[control$time == 1, ]
preControl$headline_type <- ifelse(preControl$headline <= 8, "true", "false")
preReliable<- subset(preControl, select = c(pid, reliable_, headline_type))
preReliable<- na.omit(preReliable)

# Initialize vectors to store HR and FAR values for each criterion
HR1 <- HR2 <- HR3 <- HR4 <- HR5 <- HR6 <- HR7 <- vector("numeric", length(unique(preReliable$pid)))
FAR1 <- FAR2 <- FAR3 <- FAR4 <- FAR5 <- FAR6 <- FAR7 <- vector("numeric", length(unique(preReliable$pid)))

# Calculate HRs and FARs for each participant at each criterion
for (i in 1:length(unique(preReliable$pid))) { 
  current_pid <- unique(preReliable$pid)[i]
  
  h1 = subset(preReliable$reliable_, preReliable$pid == current_pid & preReliable$headline_type == 'true' 
              & preReliable$reliable_ >= 1)
  HR1[i] = length(h1)/8
  
  fa1 = subset(preReliable$reliable_, preReliable$pid == current_pid & preReliable$headline_type == 'false' 
               & preReliable$reliable_ >= 1)
  FAR1[i] = length(fa1)/8
  
  h2 = subset(preReliable$reliable_, preReliable$pid == current_pid & preReliable$headline_type == 'true' 
              & preReliable$reliable_ >= 2)
  HR2[i] = length(h2)/8
  
  fa2 = subset(preReliable$reliable_, preReliable$pid == current_pid & preReliable$headline_type == 'false' 
               & preReliable$reliable_ >= 2)
  FAR2[i] = length(fa2)/8
  
  h3 = subset(preReliable$reliable_, preReliable$pid == current_pid & preReliable$headline_type == 'true' 
              & preReliable$reliable_ >= 3)
  HR3[i] = length(h3)/8
  
  fa3 = subset(preReliable$reliable_, preReliable$pid == current_pid & preReliable$headline_type == 'false' 
               & preReliable$reliable_ >= 3)
  FAR3[i] = length(fa3)/8
  
  h4 = subset(preReliable$reliable_, preReliable$pid == current_pid & preReliable$headline_type == 'true' 
              & preReliable$reliable_ >= 4)
  HR4[i] = length(h4)/8
  
  fa4 = subset(preReliable$reliable_, preReliable$pid == current_pid & preReliable$headline_type == 'false' 
               & preReliable$reliable_ >= 4)
  FAR4[i] = length(fa4)/8
  
  h5 = subset(preReliable$reliable_, preReliable$pid == current_pid & preReliable$headline_type == 'true' 
              & preReliable$reliable_ >= 5)
  HR5[i] = length(h5)/8
  
  fa5 = subset(preReliable$reliable_, preReliable$pid == current_pid & preReliable$headline_type == 'false' 
               & preReliable$reliable_ >= 5)
  FAR5[i] = length(fa5)/8
  
  h6 = subset(preReliable$reliable_, preReliable$pid == current_pid & preReliable$headline_type == 'true' 
              & preReliable$reliable_ >= 6)
  HR6[i] = length(h6)/8
  
  fa6 = subset(preReliable$reliable_, preReliable$pid == current_pid & preReliable$headline_type == 'false' 
               & preReliable$reliable_ >= 6)
  FAR6[i] = length(fa6)/8
  
  h7 = subset(preReliable$reliable_, preReliable$pid == current_pid & preReliable$headline_type == 'true' 
              & preReliable$reliable_ >= 7)
  HR7[i] = length(h7)/8
  
  fa7 = subset(preReliable$reliable_, preReliable$pid == current_pid & preReliable$headline_type == 'false' 
               & preReliable$reliable_ >= 7)
  FAR7[i] = length(fa7)/8
}

# Combine the HRs for each different criterion into a single data frame, do the same for FARs
pre1HRs = data.frame(HR1, HR2, HR3, HR4, HR5, HR6, HR7) 
pre1FARs = data.frame(FAR1, FAR2, FAR3, FAR4, FAR5, FAR6, FAR7)

# Calculate the mean HR and FAR at each criterion
for (i in 1:7) {
  nam = paste0("mean.HR", i)
  nam2 = paste0("mean.FAR", i)
  assign(nam, mean(pre1HRs[,i]))
  assign(nam2, mean(pre1FARs[,i]))
}

# Create a vector with the mean HRs at each criterion, do the same for FARs
mean.HR0 = 0
pretest1.HR.means = c(mean.HR1, mean.HR2, mean.HR3, mean.HR4, mean.HR5, mean.HR6, mean.HR7, mean.HR0)
mean.FAR0 = 0
pretest1.FAR.means = c(mean.FAR1, mean.FAR2, mean.FAR3, mean.FAR4, mean.FAR5, mean.FAR6, mean.FAR7, mean.FAR0)

pretest1.HR.means
pretest1.FAR.means

# Calculate the mean and sd of HRs and FARs
pretest1.mean.HR = mean(pretest1.HR.means[2:7])
pretest1.sd.HR = sd(pretest1.HR.means[2:7])
pretest1.mean.FAR = mean(pretest1.FAR.means[2:7])
pretest1.sd.FAR = sd(pretest1.FAR.means[2:7])

# Initialise variable for for loop
auc = 0

# Calculate the AUC for each pid
for (i in 1:(length(unique(preReliable$pid)))) { 
  
  auc[i] = 
    
    (.5 * (pre1HRs[i,1]+pre1HRs[i,2])*
       (pre1FARs[i,1]-pre1FARs[i,2])) +
    
    (.5 * (pre1HRs[i,2]+pre1HRs[i,3])*
       (pre1FARs[i,2]-pre1FARs[i,3])) +
    
    (.5 * (pre1HRs[i,3]+pre1HRs[i,4])*
       (pre1FARs[i,3]-pre1FARs[i,4])) +
    
    (.5 * (pre1HRs[i,4]+pre1HRs[i,5])*
       (pre1FARs[i,4]-pre1FARs[i,5])) +
    
    (.5 * (pre1HRs[i,5]+pre1HRs[i,6])*
       (pre1FARs[i,5]-pre1FARs[i,6])) +
    
    (.5 * (pre1HRs[i,6]+pre1HRs[i,7])*
       (pre1FARs[i,6]-pre1FARs[i,7])) +
    
    (.5 * (pre1HRs[i,7]+0)*
       (pre1FARs[i,7]-0)) 
  
}
pretest1.AUC = auc

# Calculate the mean and sd of AUC values
pretest1.mean.AUC = mean(pretest1.AUC)
pretest1.sd.AUC = sd(pretest1.AUC)

pretest1.mean.AUC
pretest1.sd.AUC

# Calculate HRs and FARs for each pid at each criterion with a loglinear correction
for (i in 1:length(unique(preReliable$pid))) { 
  current_pid <- unique(preReliable$pid)[i] 
  
h1 = subset(preReliable$reliable_, preReliable$pid == current_pid & preReliable$headline_type == 'true' 
              & preReliable$reliable_ >= 1)
HR1[i] = length(h1)/8

fa1 = subset(preReliable$reliable_, preReliable$pid == current_pid & preReliable$headline_type == 'false' 
              & preReliable$reliable_ >= 1)
FAR1[i] = length(fa1)/8

h2 = subset(preReliable$reliable_, preReliable$pid == current_pid & preReliable$headline_type == 'true' 
              & preReliable$reliable_ >= 2)
HR2[i] = (length(h2)+0.5)/(8+1)

fa2 = subset(preReliable$reliable_, preReliable$pid == current_pid & preReliable$headline_type == 'false' 
              & preReliable$reliable_ >= 2)
FAR2[i] = (length(fa2)+0.5)/(8+1)

h3 = subset(preReliable$reliable_, preReliable$pid == current_pid & preReliable$headline_type == 'true' 
              & preReliable$reliable_ >= 3)
HR3[i] = (length(h3)+0.5)/(8+1)

fa3 = subset(preReliable$reliable_, preReliable$pid == current_pid & preReliable$headline_type == 'false' 
              & preReliable$reliable_ >= 3)
FAR3[i] = (length(fa3)+0.5)/(8+1)

h4 = subset(preReliable$reliable_, preReliable$pid == current_pid & preReliable$headline_type == 'true' 
              & preReliable$reliable_ >= 4)
HR4[i] = (length(h4)+0.5)/(8+1)

fa4 = subset(preReliable$reliable_, preReliable$pid == current_pid & preReliable$headline_type == 'false' 
              & preReliable$reliable_ >= 4)
FAR4[i] = (length(fa4)+0.5)/(8+1)

h5 = subset(preReliable$reliable_, preReliable$pid == current_pid & preReliable$headline_type == 'true' 
              & preReliable$reliable_ >= 5)
HR5[i] = (length(h5)+0.5)/(8+1)

fa5 = subset(preReliable$reliable_, preReliable$pid == current_pid & preReliable$headline_type == 'false' 
              & preReliable$reliable_ >= 5)
FAR5[i] = (length(fa5)+0.5)/(8+1)

h6 = subset(preReliable$reliable_, preReliable$pid == current_pid & preReliable$headline_type == 'true' 
              & preReliable$reliable_ >= 6)
HR6[i] = (length(h6)+0.5)/(8+1)

fa6 = subset(preReliable$reliable_, preReliable$pid == current_pid & preReliable$headline_type == 'false' 
              & preReliable$reliable_ >= 6)
FAR6[i] = (length(fa6)+0.5)/(8+1)

h7 = subset(preReliable$reliable_, preReliable$pid == current_pid & preReliable$headline_type == 'true' 
              & preReliable$reliable_ >= 7)
HR7[i] = (length(h7)+0.5)/(8+1)

fa7 = subset(preReliable$reliable_, preReliable$pid == current_pid & preReliable$headline_type == 'false' 
              & preReliable$reliable_ >= 7)
FAR7[i] = (length(fa7)+0.5)/(8+1)
  
}

# Combine the HRs for each different criterion into a single data frame, do the same for FARs
HRs1 = data.frame(HR1, HR2, HR3, HR4, HR5, HR6, HR7) 
FARs1 = data.frame(FAR1, FAR2, FAR3, FAR4, FAR5, FAR6, FAR7)

# Calculate B"D values
BD2 = 0
BD3 = 0 
BD4 = 0
BD5 = 0
BD6 = 0
BD7 = 0

for (i in 1:(length(unique(preReliable$pid)))) {
  BD2[i] = ( ((1 - HRs1[i,2]) * (1 - FARs1[i,2])) - (HRs1[i,2]*FARs1[i,2]) ) / ( ((1 - HRs1[i,2]) * (1 - FARs1[i,2])) + (HRs1[i,2]*FARs1[i,2]) )
  BD3[i] = ( ((1 - HRs1[i,3]) * (1 - FARs1[i,3])) - (HRs1[i,3]*FARs1[i,3]) ) / ( ((1 - HRs1[i,3]) * (1 - FARs1[i,3])) + (HRs1[i,3]*FARs1[i,3]) )
  BD4[i] = ( ((1 - HRs1[i,4]) * (1 - FARs1[i,4])) - (HRs1[i,4]*FARs1[i,4]) ) / ( ((1 - HRs1[i,4]) * (1 - FARs1[i,4])) + (HRs1[i,4]*FARs1[i,4]) )
  BD5[i] = ( ((1 - HRs1[i,5]) * (1 - FARs1[i,5])) - (HRs1[i,5]*FARs1[i,5]) ) / ( ((1 - HRs1[i,5]) * (1 - FARs1[i,5])) + (HRs1[i,5]*FARs1[i,5]) )
  BD6[i] = ( ((1 - HRs1[i,6]) * (1 - FARs1[i,6])) - (HRs1[i,6]*FARs1[i,6]) ) / ( ((1 - HRs1[i,6]) * (1 - FARs1[i,6])) + (HRs1[i,6]*FARs1[i,6]) )
  BD7[i] = ( ((1 - HRs1[i,7]) * (1 - FARs1[i,7])) - (HRs1[i,7]*FARs1[i,7]) ) / ( ((1 - HRs1[i,7]) * (1 - FARs1[i,7])) + (HRs1[i,7]*FARs1[i,7]) )
}
pretest1.BD = cbind(BD2, BD3, BD4, BD5, BD6, BD7)

# Calculate the mean B"D value for each pid
pretest1.BD.means = rowMeans(pretest1.BD)

# Calculate the mean and sd of B"D values at each scale point apart from 1 
pretest1.BD.scale.means = colMeans(pretest1.BD)
pretest1.BD.scale.sds = apply(pretest1.BD,2,sd)

pretest1.BD.scale.means
pretest1.BD.scale.sds

# Calculate the mean and sd of B"D values across all scale points apart from 1
pretest1.mean.BD = mean(c(pretest1.BD))
pretest1.sd.BD = sd(c(pretest1.BD))

pretest1.mean.BD
pretest1.sd.BD
```
#Discernment Analysis Reliability-post-test
```{r}
postControl= control[control$time == 2, ]
postControl$headline_type <- ifelse(postControl$headline <= 8, "true", "false")
PostReliable<- subset(postControl, select = c(pid, reliable_, headline_type))
PostReliable<- na.omit(PostReliable)

# Initialize vectors to store HR and FAR values for each criterion
HR1 <- HR2 <- HR3 <- HR4 <- HR5 <- HR6 <- HR7 <- vector("numeric", length(unique(PostReliable$pid)))
FAR1 <- FAR2 <- FAR3 <- FAR4 <- FAR5 <- FAR6 <- FAR7 <- vector("numeric", length(unique(PostReliable$pid)))

# Calculate HRs and FARs for each pid at each criterion
# Calculate HRs and FARs for each participant at each criterion
for (i in 1:length(unique(PostReliable$pid))) { 
  current_pid <- unique(PostReliable$pid)[i]
  
  h1 = subset(PostReliable$reliable_, PostReliable$pid == current_pid & PostReliable$headline_type == 'true' 
              & PostReliable$reliable_ >= 1)
  HR1[i] = length(h1)/8
  
  fa1 = subset(PostReliable$reliable_, PostReliable$pid == current_pid & PostReliable$headline_type == 'false' 
               & PostReliable$reliable_ >= 1)
  FAR1[i] = length(fa1)/8
  
  h2 = subset(PostReliable$reliable_, PostReliable$pid == current_pid & PostReliable$headline_type == 'true' 
              & PostReliable$reliable_ >= 2)
  HR2[i] = length(h2)/8
  
  fa2 = subset(PostReliable$reliable_, PostReliable$pid == current_pid & PostReliable$headline_type == 'false' 
               & PostReliable$reliable_ >= 2)
  FAR2[i] = length(fa2)/8
  
  h3 = subset(PostReliable$reliable_, PostReliable$pid == current_pid & PostReliable$headline_type == 'true' 
              & PostReliable$reliable_ >= 3)
  HR3[i] = length(h3)/8
  
  fa3 = subset(PostReliable$reliable_, PostReliable$pid == current_pid & PostReliable$headline_type == 'false' 
               & PostReliable$reliable_ >= 3)
  FAR3[i] = length(fa3)/8
  
  h4 = subset(PostReliable$reliable_, PostReliable$pid == current_pid & PostReliable$headline_type == 'true' 
              & PostReliable$reliable_ >= 4)
  HR4[i] = length(h4)/8
  
  fa4 = subset(PostReliable$reliable_, PostReliable$pid == current_pid & PostReliable$headline_type == 'false' 
               & PostReliable$reliable_ >= 4)
  FAR4[i] = length(fa4)/8
  
  h5 = subset(PostReliable$reliable_, PostReliable$pid == current_pid & PostReliable$headline_type == 'true' 
              & PostReliable$reliable_ >= 5)
  HR5[i] = length(h5)/8
  
  fa5 = subset(PostReliable$reliable_, PostReliable$pid == current_pid & PostReliable$headline_type == 'false' 
               & PostReliable$reliable_ >= 5)
  FAR5[i] = length(fa5)/8
  
  h6 = subset(PostReliable$reliable_, PostReliable$pid == current_pid & PostReliable$headline_type == 'true' 
              & PostReliable$reliable_ >= 6)
  HR6[i] = length(h6)/8
  
  fa6 = subset(PostReliable$reliable_, PostReliable$pid == current_pid & PostReliable$headline_type == 'false' 
               & PostReliable$reliable_ >= 6)
  FAR6[i] = length(fa6)/8
  
  h7 = subset(PostReliable$reliable_, PostReliable$pid == current_pid & PostReliable$headline_type == 'true' 
              & PostReliable$reliable_ >= 7)
  HR7[i] = length(h7)/8
  
  fa7 = subset(PostReliable$reliable_, PostReliable$pid == current_pid & PostReliable$headline_type == 'false' 
               & PostReliable$reliable_ >= 7)
  FAR7[i] = length(fa7)/8
}

# Combine the HRs for each different criterion into a single data frame, do the same for FARs
post1HRs = data.frame(HR1, HR2, HR3, HR4, HR5, HR6, HR7) 
post1FARs = data.frame(FAR1, FAR2, FAR3, FAR4, FAR5, FAR6, FAR7)


# Calculate the mean HR and FAR at each criterion
for (i in 1:7) {
  nam = paste0("mean.HR", i)
  nam2 = paste0("mean.FAR", i)
  assign(nam, mean(post1HRs[,i]))
  assign(nam2, mean(post1FARs[,i]))
}

# Create a vector with the mean HRs at each criterion, do the same for FARs
mean.HR0 = 0
posttest1.HR.means = c(mean.HR1, mean.HR2, mean.HR3, mean.HR4, mean.HR5, mean.HR6, mean.HR7, mean.HR0)
mean.FAR0 = 0
posttest1.FAR.means = c(mean.FAR1, mean.FAR2, mean.FAR3, mean.FAR4, mean.FAR5, mean.FAR6, mean.FAR7, mean.FAR0)

posttest1.HR.means
posttest1.FAR.means

# Calculate the mean and sd of HRs and FARs
posttest1.mean.HR = mean(posttest1.HR.means[2:7])
posttest1.sd.HR = sd(posttest1.HR.means[2:7])
posttest1.mean.FAR = mean(posttest1.FAR.means[2:7])
posttest1.sd.FAR = sd(posttest1.FAR.means[2:7])

# Initialise variable for for loop
auc = 0

# Calculate the AUC for each pid
for (i in 1:(length(unique(PostReliable$pid)))) { 
  
  auc[i] = 
    
    (.5 * (post1HRs[i,1]+post1HRs[i,2])*
       (post1FARs[i,1]-post1FARs[i,2])) +
    
    (.5 * (post1HRs[i,2]+post1HRs[i,3])*
       (post1FARs[i,2]-post1FARs[i,3])) +
    
    (.5 * (post1HRs[i,3]+post1HRs[i,4])*
       (post1FARs[i,3]-post1FARs[i,4])) +
    
    (.5 * (post1HRs[i,4]+post1HRs[i,5])*
       (post1FARs[i,4]-post1FARs[i,5])) +
    
    (.5 * (post1HRs[i,5]+post1HRs[i,6])*
       (post1FARs[i,5]-post1FARs[i,6])) +
    
    (.5 * (post1HRs[i,6]+post1HRs[i,7])*
       (post1FARs[i,6]-post1FARs[i,7])) +
    
    (.5 * (post1HRs[i,7]+0)*
       (post1FARs[i,7]-0)) 
  
}
posttest1.AUC = auc

# Calculate the mean and sd of AUC values
posttest1.mean.AUC = mean(posttest1.AUC)
posttest1.sd.AUC = sd(posttest1.AUC)

posttest1.mean.AUC
posttest1.sd.AUC

# Calculate HRs and FARs for each pid at each criterion with a loglinear correction
for (i in 1:length(unique(PostReliable$pid))) { 
  current_pid <- unique(PostReliable$pid)[i]
  
  h1 = subset(PostReliable$reliable_, PostReliable$pid == current_pid & PostReliable$headline_type == 'true' 
              & PostReliable$reliable_ >= 1)
  HR1[i] = length(h1)/8
  
  fa1 = subset(PostReliable$reliable_, PostReliable$pid == current_pid & PostReliable$headline_type == 'false' 
               & PostReliable$reliable_ >= 1)
  FAR1[i] = length(fa1)/8
  
  h2 = subset(PostReliable$reliable_, PostReliable$pid == current_pid & PostReliable$headline_type == 'true' 
              & PostReliable$reliable_ >= 2)
  HR2[i] = (length(h2)+0.5)/(8+1)
  
  fa2 = subset(PostReliable$reliable_, PostReliable$pid == current_pid & PostReliable$headline_type == 'false' 
               & PostReliable$reliable_ >= 2)
  FAR2[i] = (length(fa2)+0.5)/(8+1)
  
  h3 = subset(PostReliable$reliable_, PostReliable$pid == current_pid & PostReliable$headline_type == 'true' 
              & PostReliable$reliable_ >= 3)
  HR3[i] = (length(h3)+0.5)/(8+1)
  
  fa3 = subset(PostReliable$reliable_, PostReliable$pid == current_pid & PostReliable$headline_type == 'false' 
               & PostReliable$reliable_ >= 3)
  FAR3[i] = (length(fa3)+0.5)/(8+1)
  
  h4 = subset(PostReliable$reliable_, PostReliable$pid == current_pid & PostReliable$headline_type == 'true' 
              & PostReliable$reliable_ >= 4)
  HR4[i] = (length(h4)+0.5)/(8+1)
  
  fa4 = subset(PostReliable$reliable_, PostReliable$pid == current_pid & PostReliable$headline_type == 'false' 
               & PostReliable$reliable_ >= 4)
  FAR4[i] = (length(fa4)+0.5)/(8+1)
  
  h5 = subset(PostReliable$reliable_, PostReliable$pid == current_pid & PostReliable$headline_type == 'true' 
              & PostReliable$reliable_ >= 5)
  HR5[i] = (length(h5)+0.5)/(8+1)
  
  fa5 = subset(PostReliable$reliable_, PostReliable$pid == current_pid & PostReliable$headline_type == 'false' 
               & PostReliable$reliable_ >= 5)
  FAR5[i] = (length(fa5)+0.5)/(8+1)
  
  h6 = subset(PostReliable$reliable_, PostReliable$pid == current_pid & PostReliable$headline_type == 'true' 
              & PostReliable$reliable_ >= 6)
  HR6[i] = (length(h6)+0.5)/(8+1)
  
  fa6 = subset(PostReliable$reliable_, PostReliable$pid == current_pid & PostReliable$headline_type == 'false' 
               & PostReliable$reliable_ >= 6)
  FAR6[i] = (length(fa6)+0.5)/(8+1)
  
  h7 = subset(PostReliable$reliable_, PostReliable$pid == current_pid & PostReliable$headline_type == 'true' 
              & PostReliable$reliable_ >= 7)
  HR7[i] = (length(h7)+0.5)/(8+1)
  
  fa7 = subset(PostReliable$reliable_, PostReliable$pid == current_pid & PostReliable$headline_type == 'false' 
               & PostReliable$reliable_ >= 7)
  FAR7[i] = (length(fa7)+0.5)/(8+1)
  
}

# Combine the HRs for each different criterion into a single data frame, do the same for FARs
HRs2 = data.frame(HR1, HR2, HR3, HR4, HR5, HR6, HR7) 
FARs2 = data.frame(FAR1, FAR2, FAR3, FAR4, FAR5, FAR6, FAR7)

# Calculate B"D values
BD12 = 0
BD13 = 0 
BD14 = 0
BD15 = 0
BD16 = 0
BD17 = 0

for (i in 1:(length(unique(PostReliable$pid)))) {
  BD12[i] = ( ((1 - HRs2[i,2]) * (1 - FARs2[i,2])) - (HRs2[i,2]*FARs2[i,2]) ) / ( ((1 - HRs2[i,2]) * (1 - FARs2[i,2])) + (HRs2[i,2]*FARs2[i,2]) )
  BD13[i] = ( ((1 - HRs2[i,3]) * (1 - FARs2[i,3])) - (HRs2[i,3]*FARs2[i,3]) ) / ( ((1 - HRs2[i,3]) * (1 - FARs2[i,3])) + (HRs2[i,3]*FARs2[i,3]) )
  BD14[i] = ( ((1 - HRs2[i,4]) * (1 - FARs2[i,4])) - (HRs2[i,4]*FARs2[i,4]) ) / ( ((1 - HRs2[i,4]) * (1 - FARs2[i,4])) + (HRs2[i,4]*FARs2[i,4]) )
  BD15[i] = ( ((1 - HRs2[i,5]) * (1 - FARs2[i,5])) - (HRs2[i,5]*FARs2[i,5]) ) / ( ((1 - HRs2[i,5]) * (1 - FARs2[i,5])) + (HRs2[i,5]*FARs2[i,5]) )
  BD16[i] = ( ((1 - HRs2[i,6]) * (1 - FARs2[i,6])) - (HRs2[i,6]*FARs2[i,6]) ) / ( ((1 - HRs2[i,6]) * (1 - FARs2[i,6])) + (HRs2[i,6]*FARs2[i,6]) )
  BD17[i] = ( ((1 - HRs2[i,7]) * (1 - FARs2[i,7])) - (HRs2[i,7]*FARs2[i,7]) ) / ( ((1 - HRs2[i,7]) * (1 - FARs2[i,7])) + (HRs2[i,7]*FARs2[i,7]) )
}
posttest1.BD = cbind(BD12, BD13, BD14, BD15, BD16, BD17)

# Calculate the mean B"D value for each pid
posttest1.BD.means = rowMeans(posttest1.BD)

# Calculate the mean and sd of B"D values at each scale point apart from 1 
posttest1.BD.scale.means = colMeans(posttest1.BD)
posttest1.BD.scale.sds = apply(posttest1.BD,2,sd)

posttest1.BD.scale.means
posttest1.BD.scale.sds

# Calculate the mean and sd of B"D values across all scale points apart from 1
posttest1.mean.BD = mean(c(posttest1.BD))
posttest1.sd.BD = sd(c(posttest1.BD))

posttest1.mean.BD
posttest1.sd.BD

```

## ROC- Reliability #####

```{r, fig.width=20, fig.height=8}
# Plot ROCs
par(mfrow = c(1, 2))
par(mar=c(4,4,4,4))

plot(pretest1.FAR.means, pretest1.HR.means, ylim=c(0,1), xlim=c(0,1), yaxs="i", xaxs="i", 
     cex.lab = 1.2, cex.axis = 1.2, cex = 1.2, cex.main = 1.4,
     xlab = "False Alarm Rate", ylab = "Hit Rate", main = "ROC plot for the Control Condition-Reliability",
     pch = 19, col = "red")
abline(coef = c(0,1))
lines(pretest1.FAR.means, pretest1.HR.means, col = "red") # ROC curve for pretest
points(posttest1.FAR.means, posttest1.HR.means, pch = 15, col = "blue", cex = 1.2)
lines(posttest1.FAR.means, posttest1.HR.means, col = "blue") # ROC curve for posttest
legend( x="bottomright", 
        legend=c("Pretest", "Posttest"),
        col=c("red","blue"), pch = c(19,15), 
        pt.cex = c(1.2,1.2), cex = 1.2, lwd=1, lty=c(1))
```

```{r}

## AUC Analysis ##


# Conduct t-test comparing AUC values at each test
t.test(posttest1.AUC, pretest1.AUC, paired= T)

# Calculate effect size
cohen.d(posttest1.AUC, pretest1.AUC, paired= T)

# Calculate Bayes factor
ttestBF(posttest1.AUC, pretest1.AUC, paired= T)

```
##sensitivity analysis
```{r}
t.test(posttest1.BD.means, pretest1.BD.means, paired= T)

# Calculate effect size
cohen.d(posttest1.BD.means, pretest1.BD.means, paired= T)

# Calculate Bayes factor
ttestBF(posttest1.BD.means, pretest1.BD.means, paired= T)
```

#Confidence in Judgment
```{r}
control = crmisinfo[crmisinfo$condition == "control",]
preControl= control[control$time == 1, ]
preControl$headline_type <- ifelse(preControl$headline <= 8, "true", "false")
preConfidence<- subset(preControl, select = c(pid, conf_, headline_type))
preConfidence<- na.omit(preConfidence)
```
##pre-test
```{r}
HR1 <- HR2 <- HR3 <- HR4 <- HR5 <- HR6 <- HR7 <- vector("numeric", length(unique(preConfidence$pid)))
FAR1 <- FAR2 <- FAR3 <- FAR4 <- FAR5 <- FAR6 <- FAR7 <- vector("numeric", length(unique(preConfidence$pid)))

# Calculate HRs and FARs for each participant at each criterion
for (i in 1:length(unique(preConfidence$pid))) { 
  current_pid <- unique(preConfidence$pid)[i]
  
  h1 = subset(preConfidence$conf_, preConfidence$pid == current_pid & preConfidence$headline_type == 'true' 
              & preConfidence$conf_ >= 1)
  HR1[i] = length(h1)/8
  
  fa1 = subset(preConfidence$conf_, preConfidence$pid == current_pid & preConfidence$headline_type == 'false' 
               & preConfidence$conf_ >= 1)
  FAR1[i] = length(fa1)/8
  
  h2 = subset(preConfidence$conf_, preConfidence$pid == current_pid & preConfidence$headline_type == 'true' 
              & preConfidence$conf_ >= 2)
  HR2[i] = length(h2)/8
  
  fa2 = subset(preConfidence$conf_, preConfidence$pid == current_pid & preConfidence$headline_type == 'false' 
               & preConfidence$conf_ >= 2)
  FAR2[i] = length(fa2)/8
  
  h3 = subset(preConfidence$conf_, preConfidence$pid == current_pid & preConfidence$headline_type == 'true' 
              & preConfidence$conf_ >= 3)
  HR3[i] = length(h3)/8
  
  fa3 = subset(preConfidence$conf_, preConfidence$pid == current_pid & preConfidence$headline_type == 'false' 
               & preConfidence$conf_ >= 3)
  FAR3[i] = length(fa3)/8
  
  h4 = subset(preConfidence$conf_, preConfidence$pid == current_pid & preConfidence$headline_type == 'true' 
              & preConfidence$conf_ >= 4)
  HR4[i] = length(h4)/8
  
  fa4 = subset(preConfidence$conf_, preConfidence$pid == current_pid & preConfidence$headline_type == 'false' 
               & preConfidence$conf_ >= 4)
  FAR4[i] = length(fa4)/8
  
  h5 = subset(preConfidence$conf_, preConfidence$pid == current_pid & preConfidence$headline_type == 'true' 
              & preConfidence$conf_ >= 5)
  HR5[i] = length(h5)/8
  
  fa5 = subset(preConfidence$conf_, preConfidence$pid == current_pid & preConfidence$headline_type == 'false' 
               & preConfidence$conf_ >= 5)
  FAR5[i] = length(fa5)/8
  
  h6 = subset(preConfidence$conf_, preConfidence$pid == current_pid & preConfidence$headline_type == 'true' 
              & preConfidence$conf_ >= 6)
  HR6[i] = length(h6)/8
  
  fa6 = subset(preConfidence$conf_, preConfidence$pid == current_pid & preConfidence$headline_type == 'false' 
               & preConfidence$conf_ >= 6)
  FAR6[i] = length(fa6)/8
  
  h7 = subset(preConfidence$conf_, preConfidence$pid == current_pid & preConfidence$headline_type == 'true' 
              & preConfidence$conf_ >= 7)
  HR7[i] = length(h7)/8
  
  fa7 = subset(preConfidence$conf_, preConfidence$pid == current_pid & preConfidence$headline_type == 'false' 
               & preConfidence$conf_ >= 7)
  FAR7[i] = length(fa7)/8
}

# Combine the HRs for each different criterion into a single data frame, do the same for FARs
pre2HRs = data.frame(HR1, HR2, HR3, HR4, HR5, HR6, HR7) 
pre2FARs = data.frame(FAR1, FAR2, FAR3, FAR4, FAR5, FAR6, FAR7)

# Calculate the mean HR and FAR at each criterion
for (i in 1:7) {
  nam = paste0("mean.HR", i)
  nam2 = paste0("mean.FAR", i)
  assign(nam, mean(pre2HRs[,i]))
  assign(nam2, mean(pre2FARs[,i]))
}

# Create a vector with the mean HRs at each criterion, do the same for FARs
mean.HR0 = 0
pretest2.HR.means = c(mean.HR1, mean.HR2, mean.HR3, mean.HR4, mean.HR5, mean.HR6, mean.HR7, mean.HR0)
mean.FAR0 = 0
pretest2.FAR.means = c(mean.FAR1, mean.FAR2, mean.FAR3, mean.FAR4, mean.FAR5, mean.FAR6, mean.FAR7, mean.FAR0)

pretest2.HR.means
pretest2.FAR.means

# Calculate the mean and sd of HRs and FARs
pretest2.mean.HR = mean(pretest2.HR.means[2:7])
pretest2.sd.HR = sd(pretest2.HR.means[2:7])
pretest2.mean.FAR = mean(pretest2.FAR.means[2:7])
pretest2.sd.FAR = sd(pretest2.FAR.means[2:7])

# Initialise variable for for loop
auc = 0

# Calculate the AUC for each pid
for (i in 1:(length(unique(preConfidence$pid)))) { 
  
  auc[i] = 
    
    (.5 * (pre2HRs[i,1]+pre2HRs[i,2])*
       (pre2FARs[i,1]-pre2FARs[i,2])) +
    
    (.5 * (pre2HRs[i,2]+pre2HRs[i,3])*
       (pre2FARs[i,2]-pre2FARs[i,3])) +
    
    (.5 * (pre2HRs[i,3]+pre2HRs[i,4])*
       (pre2FARs[i,3]-pre2FARs[i,4])) +
    
    (.5 * (pre2HRs[i,4]+pre2HRs[i,5])*
       (pre2FARs[i,4]-pre2FARs[i,5])) +
    
    (.5 * (pre2HRs[i,5]+pre2HRs[i,6])*
       (pre2FARs[i,5]-pre2FARs[i,6])) +
    
    (.5 * (pre2HRs[i,6]+pre2HRs[i,7])*
       (pre2FARs[i,6]-pre2FARs[i,7])) +
    
    (.5 * (pre2HRs[i,7]+0)*
       (pre2FARs[i,7]-0)) 
  
}
pretest2.AUC = auc

# Calculate the mean and sd of AUC values
pretest2.mean.AUC = mean(pretest2.AUC)
pretest2.sd.AUC = sd(pretest2.AUC)

pretest2.mean.AUC
pretest2.sd.AUC

# Calculate HRs and FARs for each pid at each criterion with a loglinear correction
for (i in 1:length(unique(preConfidence$pid))) { 
  current_pid <- unique(preConfidence$pid)[i] 
  
h1 = subset(preConfidence$conf_, preConfidence$pid == current_pid & preConfidence$headline_type == 'true' 
              & preConfidence$conf_ >= 1)
HR1[i] = length(h1)/8

fa1 = subset(preConfidence$conf_, preConfidence$pid == current_pid & preConfidence$headline_type == 'false' 
              & preConfidence$conf_ >= 1)
FAR1[i] = length(fa1)/8

h2 = subset(preConfidence$conf_, preConfidence$pid == current_pid & preConfidence$headline_type == 'true' 
              & preConfidence$conf_ >= 2)
HR2[i] = (length(h2)+0.5)/(8+1)

fa2 = subset(preConfidence$conf_, preConfidence$pid == current_pid & preConfidence$headline_type == 'false' 
              & preConfidence$conf_ >= 2)
FAR2[i] = (length(fa2)+0.5)/(8+1)

h3 = subset(preConfidence$conf_, preConfidence$pid == current_pid & preConfidence$headline_type == 'true' 
              & preConfidence$conf_ >= 3)
HR3[i] = (length(h3)+0.5)/(8+1)

fa3 = subset(preConfidence$conf_, preConfidence$pid == current_pid & preConfidence$headline_type == 'false' 
              & preConfidence$conf_ >= 3)
FAR3[i] = (length(fa3)+0.5)/(8+1)

h4 = subset(preConfidence$conf_, preConfidence$pid == current_pid & preConfidence$headline_type == 'true' 
              & preConfidence$conf_ >= 4)
HR4[i] = (length(h4)+0.5)/(8+1)

fa4 = subset(preConfidence$conf_, preConfidence$pid == current_pid & preConfidence$headline_type == 'false' 
              & preConfidence$conf_ >= 4)
FAR4[i] = (length(fa4)+0.5)/(8+1)

h5 = subset(preConfidence$conf_, preConfidence$pid == current_pid & preConfidence$headline_type == 'true' 
              & preConfidence$conf_ >= 5)
HR5[i] = (length(h5)+0.5)/(8+1)

fa5 = subset(preConfidence$conf_, preConfidence$pid == current_pid & preConfidence$headline_type == 'false' 
              & preConfidence$conf_ >= 5)
FAR5[i] = (length(fa5)+0.5)/(8+1)

h6 = subset(preConfidence$conf_, preConfidence$pid == current_pid & preConfidence$headline_type == 'true' 
              & preConfidence$conf_ >= 6)
HR6[i] = (length(h6)+0.5)/(8+1)

fa6 = subset(preConfidence$conf_, preConfidence$pid == current_pid & preConfidence$headline_type == 'false' 
              & preConfidence$conf_ >= 6)
FAR6[i] = (length(fa6)+0.5)/(8+1)

h7 = subset(preConfidence$conf_, preConfidence$pid == current_pid & preConfidence$headline_type == 'true' 
              & preConfidence$conf_ >= 7)
HR7[i] = (length(h7)+0.5)/(8+1)

fa7 = subset(preConfidence$conf_, preConfidence$pid == current_pid & preConfidence$headline_type == 'false' 
              & preConfidence$conf_ >= 7)
FAR7[i] = (length(fa7)+0.5)/(8+1)
  
}

# Combine the HRs for each different criterion into a single data frame, do the same for FARs
HRs3 = data.frame(HR1, HR2, HR3, HR4, HR5, HR6, HR7) 
FARs3 = data.frame(FAR1, FAR2, FAR3, FAR4, FAR5, FAR6, FAR7)

# Calculate B"D values
BD22 = 0
BD23 = 0 
BD24 = 0
BD25 = 0
BD26 = 0
BD27 = 0

for (i in 1:(length(unique(preConfidence$pid)))) {
  BD22[i] = ( ((1 - HRs3[i,2]) * (1 - FARs3[i,2])) - (HRs3[i,2]*FARs3[i,2]) ) / ( ((1 - HRs3[i,2]) * (1 - FARs3[i,2])) + (HRs3[i,2]*FARs3[i,2]) )
  BD23[i] = ( ((1 - HRs3[i,3]) * (1 - FARs3[i,3])) - (HRs3[i,3]*FARs3[i,3]) ) / ( ((1 - HRs3[i,3]) * (1 - FARs3[i,3])) + (HRs3[i,3]*FARs3[i,3]) )
  BD24[i] = ( ((1 - HRs3[i,4]) * (1 - FARs3[i,4])) - (HRs3[i,4]*FARs3[i,4]) ) / ( ((1 - HRs3[i,4]) * (1 - FARs3[i,4])) + (HRs3[i,4]*FARs3[i,4]) )
  BD25[i] = ( ((1 - HRs3[i,5]) * (1 - FARs3[i,5])) - (HRs3[i,5]*FARs3[i,5]) ) / ( ((1 - HRs3[i,5]) * (1 - FARs3[i,5])) + (HRs3[i,5]*FARs3[i,5]) )
  BD26[i] = ( ((1 - HRs3[i,6]) * (1 - FARs3[i,6])) - (HRs3[i,6]*FARs3[i,6]) ) / ( ((1 - HRs3[i,6]) * (1 - FARs3[i,6])) + (HRs3[i,6]*FARs3[i,6]) )
  BD27[i] = ( ((1 - HRs3[i,7]) * (1 - FARs3[i,7])) - (HRs3[i,7]*FARs3[i,7]) ) / ( ((1 - HRs3[i,7]) * (1 - FARs3[i,7])) + (HRs3[i,7]*FARs3[i,7]) )
}
pretest2.BD = cbind(BD22, BD23, BD24, BD25, BD26, BD27)

# Calculate the mean B"D value for each pid
pretest2.BD.means = rowMeans(pretest2.BD)

# Calculate the mean and sd of B"D values at each scale point apart from 1 
pretest2.BD.scale.means = colMeans(pretest2.BD)
pretest2.BD.scale.sds = apply(pretest2.BD,2,sd)

pretest2.BD.scale.means
pretest2.BD.scale.sds

# Calculate the mean and sd of B"D values across all scale points apart from 1
pretest2.mean.BD = mean(c(pretest2.BD))
pretest2.sd.BD = sd(c(pretest2.BD))

pretest2.mean.BD
pretest2.sd.BD

```
#Discernment Analysis Reliability-post-test
```{r}
postControl= control[control$time == 2, ]
postControl$headline_type <- ifelse(postControl$headline <= 8, "true", "false")
PostConfidence<- subset(postControl, select = c(pid, conf_, headline_type))
PostConfidence<- na.omit(PostConfidence)

# Initialize vectors to store HR and FAR values for each criterion
HR1 <- HR2 <- HR3 <- HR4 <- HR5 <- HR6 <- HR7 <- vector("numeric", length(unique(PostConfidence$pid)))
FAR1 <- FAR2 <- FAR3 <- FAR4 <- FAR5 <- FAR6 <- FAR7 <- vector("numeric", length(unique(PostConfidence$pid)))

# Calculate HRs and FARs for each pid at each criterion
# Calculate HRs and FARs for each participant at each criterion
for (i in 1:length(unique(PostConfidence$pid))) { 
  current_pid <- unique(PostConfidence$pid)[i]
  
  h1 = subset(PostConfidence$conf_, PostConfidence$pid == current_pid & PostConfidence$headline_type == 'true' 
              & PostConfidence$conf_ >= 1)
  HR1[i] = length(h1)/8
  
  fa1 = subset(PostConfidence$conf_, PostConfidence$pid == current_pid & PostConfidence$headline_type == 'false' 
               & PostConfidence$conf_ >= 1)
  FAR1[i] = length(fa1)/8
  
  h2 = subset(PostConfidence$conf_, PostConfidence$pid == current_pid & PostConfidence$headline_type == 'true' 
              & PostConfidence$conf_ >= 2)
  HR2[i] = length(h2)/8
  
  fa2 = subset(PostConfidence$conf_, PostConfidence$pid == current_pid & PostConfidence$headline_type == 'false' 
               & PostConfidence$conf_ >= 2)
  FAR2[i] = length(fa2)/8
  
  h3 = subset(PostConfidence$conf_, PostConfidence$pid == current_pid & PostConfidence$headline_type == 'true' 
              & PostConfidence$conf_ >= 3)
  HR3[i] = length(h3)/8
  
  fa3 = subset(PostConfidence$conf_, PostConfidence$pid == current_pid & PostConfidence$headline_type == 'false' 
               & PostConfidence$conf_ >= 3)
  FAR3[i] = length(fa3)/8
  
  h4 = subset(PostConfidence$conf_, PostConfidence$pid == current_pid & PostConfidence$headline_type == 'true' 
              & PostConfidence$conf_ >= 4)
  HR4[i] = length(h4)/8
  
  fa4 = subset(PostConfidence$conf_, PostConfidence$pid == current_pid & PostConfidence$headline_type == 'false' 
               & PostConfidence$conf_ >= 4)
  FAR4[i] = length(fa4)/8
  
  h5 = subset(PostConfidence$conf_, PostConfidence$pid == current_pid & PostConfidence$headline_type == 'true' 
              & PostConfidence$conf_ >= 5)
  HR5[i] = length(h5)/8
  
  fa5 = subset(PostConfidence$conf_, PostConfidence$pid == current_pid & PostConfidence$headline_type == 'false' 
               & PostConfidence$conf_ >= 5)
  FAR5[i] = length(fa5)/8
  
  h6 = subset(PostConfidence$conf_, PostConfidence$pid == current_pid & PostConfidence$headline_type == 'true' 
              & PostConfidence$conf_ >= 6)
  HR6[i] = length(h6)/8
  
  fa6 = subset(PostConfidence$conf_, PostConfidence$pid == current_pid & PostConfidence$headline_type == 'false' 
               & PostConfidence$conf_ >= 6)
  FAR6[i] = length(fa6)/8
  
  h7 = subset(PostConfidence$conf_, PostConfidence$pid == current_pid & PostConfidence$headline_type == 'true' 
              & PostConfidence$conf_ >= 7)
  HR7[i] = length(h7)/8
  
  fa7 = subset(PostConfidence$conf_, PostConfidence$pid == current_pid & PostConfidence$headline_type == 'false' 
               & PostConfidence$conf_ >= 7)
  FAR7[i] = length(fa7)/8
}

# Combine the HRs for each different criterion into a single data frame, do the same for FARs
post2HRs = data.frame(HR1, HR2, HR3, HR4, HR5, HR6, HR7) 
post2FARs = data.frame(FAR1, FAR2, FAR3, FAR4, FAR5, FAR6, FAR7)


# Calculate the mean HR and FAR at each criterion
for (i in 1:7) {
  nam = paste0("mean.HR", i)
  nam2 = paste0("mean.FAR", i)
  assign(nam, mean(post2HRs[,i]))
  assign(nam2, mean(post2FARs[,i]))
}

# Create a vector with the mean HRs at each criterion, do the same for FARs
mean.HR0 = 0
posttest2.HR.means = c(mean.HR1, mean.HR2, mean.HR3, mean.HR4, mean.HR5, mean.HR6, mean.HR7, mean.HR0)
mean.FAR0 = 0
posttest2.FAR.means = c(mean.FAR1, mean.FAR2, mean.FAR3, mean.FAR4, mean.FAR5, mean.FAR6, mean.FAR7, mean.FAR0)

posttest2.HR.means
posttest2.FAR.means

# Calculate the mean and sd of HRs and FARs
posttest2.mean.HR = mean(posttest2.HR.means[2:7])
posttest2.sd.HR = sd(posttest2.HR.means[2:7])
posttest2.mean.FAR = mean(posttest2.FAR.means[2:7])
posttest2.sd.FAR = sd(posttest2.FAR.means[2:7])

# Initialise variable for for loop
auc = 0

# Calculate the AUC for each pid
for (i in 1:(length(unique(PostConfidence$pid)))) { 
  
  auc[i] = 
    
    (.5 * (post2HRs[i,1]+post2HRs[i,2])*
       (post2FARs[i,1]-post2FARs[i,2])) +
    
    (.5 * (post2HRs[i,2]+post2HRs[i,3])*
       (post2FARs[i,2]-post2FARs[i,3])) +
    
    (.5 * (post2HRs[i,3]+post2HRs[i,4])*
       (post2FARs[i,3]-post2FARs[i,4])) +
    
    (.5 * (post2HRs[i,4]+post2HRs[i,5])*
       (post2FARs[i,4]-post2FARs[i,5])) +
    
    (.5 * (post2HRs[i,5]+post2HRs[i,6])*
       (post2FARs[i,5]-post2FARs[i,6])) +
    
    (.5 * (post2HRs[i,6]+post2HRs[i,7])*
       (post2FARs[i,6]-post2FARs[i,7])) +
    
    (.5 * (post2HRs[i,7]+0)*
       (post2FARs[i,7]-0)) 
  
}
posttest2.AUC = auc

# Calculate the mean and sd of AUC values
posttest2.mean.AUC = mean(posttest2.AUC)
posttest2.sd.AUC = sd(posttest2.AUC)

posttest2.mean.AUC
posttest2.sd.AUC

# Calculate HRs and FARs for each pid at each criterion with a loglinear correction
for (i in 1:length(unique(PostConfidence$pid))) { 
  current_pid <- unique(PostConfidence$pid)[i]
  
  h1 = subset(PostConfidence$conf_, PostConfidence$pid == current_pid & PostConfidence$headline_type == 'true' 
              & PostConfidence$conf_ >= 1)
  HR1[i] = length(h1)/8
  
  fa1 = subset(PostConfidence$conf_, PostConfidence$pid == current_pid & PostConfidence$headline_type == 'false' 
               & PostConfidence$conf_ >= 1)
  FAR1[i] = length(fa1)/8
  
  h2 = subset(PostConfidence$conf_, PostConfidence$pid == current_pid & PostConfidence$headline_type == 'true' 
              & PostConfidence$conf_ >= 2)
  HR2[i] = (length(h2)+0.5)/(8+1)
  
  fa2 = subset(PostConfidence$conf_, PostConfidence$pid == current_pid & PostConfidence$headline_type == 'false' 
               & PostConfidence$conf_ >= 2)
  FAR2[i] = (length(fa2)+0.5)/(8+1)
  
  h3 = subset(PostConfidence$conf_, PostConfidence$pid == current_pid & PostConfidence$headline_type == 'true' 
              & PostConfidence$conf_ >= 3)
  HR3[i] = (length(h3)+0.5)/(8+1)
  
  fa3 = subset(PostConfidence$conf_, PostConfidence$pid == current_pid & PostConfidence$headline_type == 'false' 
               & PostConfidence$conf_ >= 3)
  FAR3[i] = (length(fa3)+0.5)/(8+1)
  
  h4 = subset(PostConfidence$conf_, PostConfidence$pid == current_pid & PostConfidence$headline_type == 'true' 
              & PostConfidence$conf_ >= 4)
  HR4[i] = (length(h4)+0.5)/(8+1)
  
  fa4 = subset(PostConfidence$conf_, PostConfidence$pid == current_pid & PostConfidence$headline_type == 'false' 
               & PostConfidence$conf_ >= 4)
  FAR4[i] = (length(fa4)+0.5)/(8+1)
  
  h5 = subset(PostConfidence$conf_, PostConfidence$pid == current_pid & PostConfidence$headline_type == 'true' 
              & PostConfidence$conf_ >= 5)
  HR5[i] = (length(h5)+0.5)/(8+1)
  
  fa5 = subset(PostConfidence$conf_, PostConfidence$pid == current_pid & PostConfidence$headline_type == 'false' 
               & PostConfidence$conf_ >= 5)
  FAR5[i] = (length(fa5)+0.5)/(8+1)
  
  h6 = subset(PostConfidence$conf_, PostConfidence$pid == current_pid & PostConfidence$headline_type == 'true' 
              & PostConfidence$conf_ >= 6)
  HR6[i] = (length(h6)+0.5)/(8+1)
  
  fa6 = subset(PostConfidence$conf_, PostConfidence$pid == current_pid & PostConfidence$headline_type == 'false' 
               & PostConfidence$conf_ >= 6)
  FAR6[i] = (length(fa6)+0.5)/(8+1)
  
  h7 = subset(PostConfidence$conf_, PostConfidence$pid == current_pid & PostConfidence$headline_type == 'true' 
              & PostConfidence$conf_ >= 7)
  HR7[i] = (length(h7)+0.5)/(8+1)
  
  fa7 = subset(PostConfidence$conf_, PostConfidence$pid == current_pid & PostConfidence$headline_type == 'false' 
               & PostConfidence$conf_ >= 7)
  FAR7[i] = (length(fa7)+0.5)/(8+1)
  
}

# Combine the HRs for each different criterion into a single data frame, do the same for FARs
HRs4 = data.frame(HR1, HR2, HR3, HR4, HR5, HR6, HR7) 
FARs4 = data.frame(FAR1, FAR2, FAR3, FAR4, FAR5, FAR6, FAR7)

# Calculate B"D values
BD32 = 0
BD33 = 0 
BD34 = 0
BD35 = 0
BD36 = 0
BD37 = 0

for (i in 1:(length(unique(PostConfidence$pid)))) {
  BD32[i] = ( ((1 - HRs4[i,2]) * (1 - FARs4[i,2])) - (HRs4[i,2]*FARs4[i,2]) ) / ( ((1 - HRs4[i,2]) * (1 - FARs4[i,2])) + (HRs4[i,2]*FARs4[i,2]) )
  BD33[i] = ( ((1 - HRs4[i,3]) * (1 - FARs4[i,3])) - (HRs4[i,3]*FARs4[i,3]) ) / ( ((1 - HRs4[i,3]) * (1 - FARs4[i,3])) + (HRs4[i,3]*FARs4[i,3]) )
  BD34[i] = ( ((1 - HRs4[i,4]) * (1 - FARs4[i,4])) - (HRs4[i,4]*FARs4[i,4]) ) / ( ((1 - HRs4[i,4]) * (1 - FARs4[i,4])) + (HRs4[i,4]*FARs4[i,4]) )
  BD35[i] = ( ((1 - HRs4[i,5]) * (1 - FARs4[i,5])) - (HRs4[i,5]*FARs4[i,5]) ) / ( ((1 - HRs4[i,5]) * (1 - FARs4[i,5])) + (HRs4[i,5]*FARs4[i,5]) )
  BD36[i] = ( ((1 - HRs4[i,6]) * (1 - FARs4[i,6])) - (HRs4[i,6]*FARs4[i,6]) ) / ( ((1 - HRs4[i,6]) * (1 - FARs4[i,6])) + (HRs4[i,6]*FARs4[i,6]) )
  BD37[i] = ( ((1 - HRs4[i,7]) * (1 - FARs4[i,7])) - (HRs4[i,7]*FARs4[i,7]) ) / ( ((1 - HRs4[i,7]) * (1 - FARs4[i,7])) + (HRs4[i,7]*FARs4[i,7]) )
}
posttest2.BD = cbind(BD12, BD13, BD14, BD15, BD16, BD17)

# Calculate the mean B"D value for each pid
posttest2.BD.means = rowMeans(posttest2.BD)

# Calculate the mean and sd of B"D values at each scale point apart from 1 
posttest2.BD.scale.means = colMeans(posttest2.BD)
posttest2.BD.scale.sds = apply(posttest2.BD,2,sd)

posttest2.BD.scale.means
posttest2.BD.scale.sds

# Calculate the mean and sd of B"D values across all scale points apart from 1
posttest2.mean.BD = mean(c(posttest2.BD))
posttest2.sd.BD = sd(c(posttest2.BD))

posttest2.mean.BD
posttest2.sd.BD

```
## ROC- Confidence #####

```{r, fig.width=20, fig.height=8}
# Plot ROCs
par(mfrow = c(1, 2))
par(mar=c(4,4,4,4))

plot(pretest2.FAR.means, pretest2.HR.means, ylim=c(0,1), xlim=c(0,1), yaxs="i", xaxs="i", 
     cex.lab = 1.2, cex.axis = 1.2, cex = 1.2, cex.main = 1.4,
     xlab = "False Alarm Rate", ylab = "Hit Rate", main = "ROC plot for the Control Condition-Confidence in Judgment",
     pch = 19, col = "red")
abline(coef = c(0,1))
lines(pretest2.FAR.means, pretest2.HR.means, col = "red") # ROC curve for pretest
points(posttest2.FAR.means, posttest2.HR.means, pch = 15, col = "blue", cex = 1.2)
lines(posttest2.FAR.means, posttest2.HR.means, col = "blue") # ROC curve for posttest
legend( x="bottomright", 
        legend=c("Pretest", "Posttest"),
        col=c("red","blue"), pch = c(19,15), 
        pt.cex = c(1.2,1.2), cex = 1.2, lwd=1, lty=c(1))

```

```{r}

## AUC Analysis ##

# Conduct t-test comparing AUC values at each test
t.test(posttest2.AUC, pretest2.AUC, paired= T)

# Calculate effect size
cohen.d(posttest2.AUC, pretest2.AUC, paired= T)

# Calculate Bayes factor
ttestBF(posttest2.AUC, pretest2.AUC, paired= T)

```
#sensitivity analysis
```{r}
t.test(posttest2.BD.means, pretest2.BD.means, paired= T)

# Calculate effect size
cohen.d(posttest2.BD.means, pretest2.BD.means, paired= T)

# Calculate Bayes factor
ttestBF(posttest2.BD.means, pretest2.BD.means, paired= T)
```
#Sharing Intention
```{r}
control = crmisinfo[crmisinfo$condition == "control",]
preControl= control[control$time == 1, ]
preControl$headline_type <- ifelse(preControl$headline <= 8, "true", "false")
preShare<- subset(preControl, select = c(pid, share_, headline_type))
preShare<- na.omit(preShare)
```

##pre-test
```{r}
HR1 <- HR2 <- HR3 <- HR4 <- HR5 <- HR6 <- HR7 <- vector("numeric", length(unique(preShare$pid)))
FAR1 <- FAR2 <- FAR3 <- FAR4 <- FAR5 <- FAR6 <- FAR7 <- vector("numeric", length(unique(preShare$pid)))

# Calculate HRs and FARs for each participant at each criterion
for (i in 1:length(unique(preShare$pid))) { 
  current_pid <- unique(preShare$pid)[i]
  
  h1 = subset(preShare$share_, preShare$pid == current_pid & preShare$headline_type == 'true' 
              & preShare$share_ >= 1)
  HR1[i] = length(h1)/8
  
  fa1 = subset(preShare$share_, preShare$pid == current_pid & preShare$headline_type == 'false' 
               & preShare$share_ >= 1)
  FAR1[i] = length(fa1)/8
  
  h2 = subset(preShare$share_, preShare$pid == current_pid & preShare$headline_type == 'true' 
              & preShare$share_ >= 2)
  HR2[i] = length(h2)/8
  
  fa2 = subset(preShare$share_, preShare$pid == current_pid & preShare$headline_type == 'false' 
               & preShare$share_ >= 2)
  FAR2[i] = length(fa2)/8
  
  h3 = subset(preShare$share_, preShare$pid == current_pid & preShare$headline_type == 'true' 
              & preShare$share_ >= 3)
  HR3[i] = length(h3)/8
  
  fa3 = subset(preShare$share_, preShare$pid == current_pid & preShare$headline_type == 'false' 
               & preShare$share_ >= 3)
  FAR3[i] = length(fa3)/8
  
  h4 = subset(preShare$share_, preShare$pid == current_pid & preShare$headline_type == 'true' 
              & preShare$share_ >= 4)
  HR4[i] = length(h4)/8
  
  fa4 = subset(preShare$share_, preShare$pid == current_pid & preShare$headline_type == 'false' 
               & preShare$share_ >= 4)
  FAR4[i] = length(fa4)/8
  
  h5 = subset(preShare$share_, preShare$pid == current_pid & preShare$headline_type == 'true' 
              & preShare$share_ >= 5)
  HR5[i] = length(h5)/8
  
  fa5 = subset(preShare$share_, preShare$pid == current_pid & preShare$headline_type == 'false' 
               & preShare$share_ >= 5)
  FAR5[i] = length(fa5)/8
  
  h6 = subset(preShare$share_, preShare$pid == current_pid & preShare$headline_type == 'true' 
              & preShare$share_ >= 6)
  HR6[i] = length(h6)/8
  
  fa6 = subset(preShare$share_, preShare$pid == current_pid & preShare$headline_type == 'false' 
               & preShare$share_ >= 6)
  FAR6[i] = length(fa6)/8
  
  h7 = subset(preShare$share_, preShare$pid == current_pid & preShare$headline_type == 'true' 
              & preShare$share_ >= 7)
  HR7[i] = length(h7)/8
  
  fa7 = subset(preShare$share_, preShare$pid == current_pid & preShare$headline_type == 'false' 
               & preShare$share_ >= 7)
  FAR7[i] = length(fa7)/8
}

# Combine the HRs for each different criterion into a single data frame, do the same for FARs
pre3HRs = data.frame(HR1, HR2, HR3, HR4, HR5, HR6, HR7) 
pre3FARs = data.frame(FAR1, FAR2, FAR3, FAR4, FAR5, FAR6, FAR7)

# Calculate the mean HR and FAR at each criterion
for (i in 1:7) {
  nam = paste0("mean.HR", i)
  nam2 = paste0("mean.FAR", i)
  assign(nam, mean(pre3HRs[,i]))
  assign(nam2, mean(pre3FARs[,i]))
}

# Create a vector with the mean HRs at each criterion, do the same for FARs
mean.HR0 = 0
pretest3.HR.means = c(mean.HR1, mean.HR2, mean.HR3, mean.HR4, mean.HR5, mean.HR6, mean.HR7, mean.HR0)
mean.FAR0 = 0
pretest3.FAR.means = c(mean.FAR1, mean.FAR2, mean.FAR3, mean.FAR4, mean.FAR5, mean.FAR6, mean.FAR7, mean.FAR0)

pretest3.HR.means
pretest3.FAR.means

# Calculate the mean and sd of HRs and FARs
pretest3.mean.HR = mean(pretest3.HR.means[2:7])
pretest3.sd.HR = sd(pretest3.HR.means[2:7])
pretest3.mean.FAR = mean(pretest3.FAR.means[2:7])
pretest3.sd.FAR = sd(pretest3.FAR.means[2:7])

# Initialise variable for for loop
auc = 0

# Calculate the AUC for each pid
for (i in 1:(length(unique(preShare$pid)))) { 
  
  auc[i] = 
    
    (.5 * (pre3HRs[i,1]+pre3HRs[i,2])*
       (pre3FARs[i,1]-pre3FARs[i,2])) +
    
    (.5 * (pre3HRs[i,2]+pre3HRs[i,3])*
       (pre3FARs[i,2]-pre3FARs[i,3])) +
    
    (.5 * (pre3HRs[i,3]+pre3HRs[i,4])*
       (pre3FARs[i,3]-pre3FARs[i,4])) +
    
    (.5 * (pre3HRs[i,4]+pre3HRs[i,5])*
       (pre3FARs[i,4]-pre3FARs[i,5])) +
    
    (.5 * (pre3HRs[i,5]+pre3HRs[i,6])*
       (pre3FARs[i,5]-pre3FARs[i,6])) +
    
    (.5 * (pre3HRs[i,6]+pre3HRs[i,7])*
       (pre3FARs[i,6]-pre3FARs[i,7])) +
    
    (.5 * (pre3HRs[i,7]+0)*
       (pre3FARs[i,7]-0)) 
  
}
pretest3.AUC = auc

# Calculate the mean and sd of AUC values
pretest3.mean.AUC = mean(pretest3.AUC)
pretest3.sd.AUC = sd(pretest3.AUC)

pretest3.mean.AUC
pretest3.sd.AUC

# Calculate HRs and FARs for each pid at each criterion with a loglinear correction
for (i in 1:length(unique(preShare$pid))) { 
  current_pid <- unique(preShare$pid)[i] 
  
  h1 = subset(preShare$share_, preShare$pid == current_pid & preShare$headline_type == 'true' 
              & preShare$share_ >= 1)
  HR1[i] = length(h1)/8
  
  fa1 = subset(preShare$share_, preShare$pid == current_pid & preShare$headline_type == 'false' 
               & preShare$share_ >= 1)
  FAR1[i] = length(fa1)/8
  
  h2 = subset(preShare$share_, preShare$pid == current_pid & preShare$headline_type == 'true' 
              & preShare$share_ >= 2)
  HR2[i] = (length(h2)+0.5)/(8+1)
  
  fa2 = subset(preShare$share_, preShare$pid == current_pid & preShare$headline_type == 'false' 
               & preShare$share_ >= 2)
  FAR2[i] = (length(fa2)+0.5)/(8+1)
  
  h3 = subset(preShare$share_, preShare$pid == current_pid & preShare$headline_type == 'true' 
              & preShare$share_ >= 3)
  HR3[i] = (length(h3)+0.5)/(8+1)
  
  fa3 = subset(preShare$share_, preShare$pid == current_pid & preShare$headline_type == 'false' 
               & preShare$share_ >= 3)
  FAR3[i] = (length(fa3)+0.5)/(8+1)
  
  h4 = subset(preShare$share_, preShare$pid == current_pid & preShare$headline_type == 'true' 
              & preShare$share_ >= 4)
  HR4[i] = (length(h4)+0.5)/(8+1)
  
  fa4 = subset(preShare$share_, preShare$pid == current_pid & preShare$headline_type == 'false' 
               & preShare$share_ >= 4)
  FAR4[i] = (length(fa4)+0.5)/(8+1)
  
  h5 = subset(preShare$share_, preShare$pid == current_pid & preShare$headline_type == 'true' 
              & preShare$share_ >= 5)
  HR5[i] = (length(h5)+0.5)/(8+1)
  
  fa5 = subset(preShare$share_, preShare$pid == current_pid & preShare$headline_type == 'false' 
               & preShare$share_ >= 5)
  FAR5[i] = (length(fa5)+0.5)/(8+1)
  
  h6 = subset(preShare$share_, preShare$pid == current_pid & preShare$headline_type == 'true' 
              & preShare$share_ >= 6)
  HR6[i] = (length(h6)+0.5)/(8+1)
  
  fa6 = subset(preShare$share_, preShare$pid == current_pid & preShare$headline_type == 'false' 
               & preShare$share_ >= 6)
  FAR6[i] = (length(fa6)+0.5)/(8+1)
  
  h7 = subset(preShare$share_, preShare$pid == current_pid & preShare$headline_type == 'true' 
              & preShare$share_ >= 7)
  HR7[i] = (length(h7)+0.5)/(8+1)
  
  fa7 = subset(preShare$share_, preShare$pid == current_pid & preShare$headline_type == 'false' 
               & preShare$share_ >= 7)
  FAR7[i] = (length(fa7)+0.5)/(8+1)
  
}

# Combine the HRs for each different criterion into a single data frame, do the same for FARs
HRs5 = data.frame(HR1, HR2, HR3, HR4, HR5, HR6, HR7) 
FARs5 = data.frame(FAR1, FAR2, FAR3, FAR4, FAR5, FAR6, FAR7)

# Calculate B"D values
BD42 = 0
BD43 = 0 
BD44 = 0
BD45 = 0
BD46 = 0
BD47 = 0

for (i in 1:(length(unique(preShare$pid)))) {
  BD42[i] = ( ((1 - HRs5[i,2]) * (1 - FARs5[i,2])) - (HRs5[i,2]*FARs5[i,2]) ) / ( ((1 - HRs5[i,2]) * (1 - FARs5[i,2])) + (HRs5[i,2]*FARs5[i,2]) )
  BD43[i] = ( ((1 - HRs5[i,3]) * (1 - FARs5[i,3])) - (HRs5[i,3]*FARs5[i,3]) ) / ( ((1 - HRs5[i,3]) * (1 - FARs5[i,3])) + (HRs5[i,3]*FARs5[i,3]) )
  BD44[i] = ( ((1 - HRs5[i,4]) * (1 - FARs5[i,4])) - (HRs5[i,4]*FARs5[i,4]) ) / ( ((1 - HRs5[i,4]) * (1 - FARs5[i,4])) + (HRs5[i,4]*FARs5[i,4]) )
  BD45[i] = ( ((1 - HRs5[i,5]) * (1 - FARs5[i,5])) - (HRs5[i,5]*FARs5[i,5]) ) / ( ((1 - HRs5[i,5]) * (1 - FARs5[i,5])) + (HRs5[i,5]*FARs5[i,5]) )
  BD46[i] = ( ((1 - HRs5[i,6]) * (1 - FARs5[i,6])) - (HRs5[i,6]*FARs5[i,6]) ) / ( ((1 - HRs5[i,6]) * (1 - FARs5[i,6])) + (HRs5[i,6]*FARs5[i,6]) )
  BD47[i] = ( ((1 - HRs5[i,7]) * (1 - FARs5[i,7])) - (HRs5[i,7]*FARs5[i,7]) ) / ( ((1 - HRs5[i,7]) * (1 - FARs5[i,7])) + (HRs5[i,7]*FARs5[i,7]) )
}
pretest3.BD = cbind(BD42, BD43, BD44, BD45, BD46, BD47)

# Calculate the mean B"D value for each pid
pretest3.BD.means = rowMeans(pretest3.BD)

# Calculate the mean and sd of B"D values at each scale point apart from 1 
pretest3.BD.scale.means = colMeans(pretest3.BD)
pretest3.BD.scale.sds = apply(pretest3.BD,2,sd)

pretest3.BD.scale.means
pretest3.BD.scale.sds

# Calculate the mean and sd of B"D values across all scale points apart from 1
pretest3.mean.BD = mean(c(pretest3.BD))
pretest3.sd.BD = sd(c(pretest3.BD))

pretest3.mean.BD
pretest3.sd.BD
```
##post-test
```{r}
postControl= control[control$time == 2, ]
postControl$headline_type <- ifelse(postControl$headline <= 8, "true", "false")
PostShare<- subset(postControl, select = c(pid, share_, headline_type))
PostShare<- na.omit(PostShare)

# Initialize vectors to store HR and FAR values for each criterion
HR1 <- HR2 <- HR3 <- HR4 <- HR5 <- HR6 <- HR7 <- vector("numeric", length(unique(PostShare$pid)))
FAR1 <- FAR2 <- FAR3 <- FAR4 <- FAR5 <- FAR6 <- FAR7 <- vector("numeric", length(unique(PostShare$pid)))

# Calculate HRs and FARs for each pid at each criterion
# Calculate HRs and FARs for each participant at each criterion
for (i in 1:length(unique(PostShare$pid))) { 
  current_pid <- unique(PostShare$pid)[i]
  
  h1 = subset(PostShare$share_, PostShare$pid == current_pid & PostShare$headline_type == 'true' 
              & PostShare$share_ >= 1)
  HR1[i] = length(h1)/8
  
  fa1 = subset(PostShare$share_, PostShare$pid == current_pid & PostShare$headline_type == 'false' 
               & PostShare$share_ >= 1)
  FAR1[i] = length(fa1)/8
  
  h2 = subset(PostShare$share_, PostShare$pid == current_pid & PostShare$headline_type == 'true' 
              & PostShare$share_ >= 2)
  HR2[i] = length(h2)/8
  
  fa2 = subset(PostShare$share_, PostShare$pid == current_pid & PostShare$headline_type == 'false' 
               & PostShare$share_ >= 2)
  FAR2[i] = length(fa2)/8
  
  h3 = subset(PostShare$share_, PostShare$pid == current_pid & PostShare$headline_type == 'true' 
              & PostShare$share_ >= 3)
  HR3[i] = length(h3)/8
  
  fa3 = subset(PostShare$share_, PostShare$pid == current_pid & PostShare$headline_type == 'false' 
               & PostShare$share_ >= 3)
  FAR3[i] = length(fa3)/8
  
  h4 = subset(PostShare$share_, PostShare$pid == current_pid & PostShare$headline_type == 'true' 
              & PostShare$share_ >= 4)
  HR4[i] = length(h4)/8
  
  fa4 = subset(PostShare$share_, PostShare$pid == current_pid & PostShare$headline_type == 'false' 
               & PostShare$share_ >= 4)
  FAR4[i] = length(fa4)/8
  
  h5 = subset(PostShare$share_, PostShare$pid == current_pid & PostShare$headline_type == 'true' 
              & PostShare$share_ >= 5)
  HR5[i] = length(h5)/8
  
  fa5 = subset(PostShare$share_, PostShare$pid == current_pid & PostShare$headline_type == 'false' 
               & PostShare$share_ >= 5)
  FAR5[i] = length(fa5)/8
  
  h6 = subset(PostShare$share_, PostShare$pid == current_pid & PostShare$headline_type == 'true' 
              & PostShare$share_ >= 6)
  HR6[i] = length(h6)/8
  
  fa6 = subset(PostShare$share_, PostShare$pid == current_pid & PostShare$headline_type == 'false' 
               & PostShare$share_ >= 6)
  FAR6[i] = length(fa6)/8
  
  h7 = subset(PostShare$share_, PostShare$pid == current_pid & PostShare$headline_type == 'true' 
              & PostShare$share_ >= 7)
  HR7[i] = length(h7)/8
  
  fa7 = subset(PostShare$share_, PostShare$pid == current_pid & PostShare$headline_type == 'false' 
               & PostShare$share_ >= 7)
  FAR7[i] = length(fa7)/8
}

# Combine the HRs for each different criterion into a single data frame, do the same for FARs
post3HRs = data.frame(HR1, HR2, HR3, HR4, HR5, HR6, HR7) 
post3FARs = data.frame(FAR1, FAR2, FAR3, FAR4, FAR5, FAR6, FAR7)


# Calculate the mean HR and FAR at each criterion
for (i in 1:7) {
  nam = paste0("mean.HR", i)
  nam2 = paste0("mean.FAR", i)
  assign(nam, mean(post3HRs[,i]))
  assign(nam2, mean(post3FARs[,i]))
}

# Create a vector with the mean HRs at each criterion, do the same for FARs
mean.HR0 = 0
posttest3.HR.means = c(mean.HR1, mean.HR2, mean.HR3, mean.HR4, mean.HR5, mean.HR6, mean.HR7, mean.HR0)
mean.FAR0 = 0
posttest3.FAR.means = c(mean.FAR1, mean.FAR2, mean.FAR3, mean.FAR4, mean.FAR5, mean.FAR6, mean.FAR7, mean.FAR0)

posttest3.HR.means
posttest3.FAR.means

# Calculate the mean and sd of HRs and FARs
posttest3.mean.HR = mean(posttest3.HR.means[2:7])
posttest3.sd.HR = sd(posttest3.HR.means[2:7])
posttest3.mean.FAR = mean(posttest3.FAR.means[2:7])
posttest3.sd.FAR = sd(posttest3.FAR.means[2:7])

# Initialise variable for for loop
auc = 0

# Calculate the AUC for each pid
for (i in 1:(length(unique(PostShare$pid)))) { 
  
  auc[i] = 
    
    (.5 * (post3HRs[i,1]+post3HRs[i,2])*
       (post3FARs[i,1]-post3FARs[i,2])) +
    
    (.5 * (post3HRs[i,2]+post3HRs[i,3])*
       (post3FARs[i,2]-post3FARs[i,3])) +
    
    (.5 * (post3HRs[i,3]+post3HRs[i,4])*
       (post3FARs[i,3]-post3FARs[i,4])) +
    
    (.5 * (post3HRs[i,4]+post3HRs[i,5])*
       (post3FARs[i,4]-post3FARs[i,5])) +
    
    (.5 * (post3HRs[i,5]+post3HRs[i,6])*
       (post3FARs[i,5]-post3FARs[i,6])) +
    
    (.5 * (post3HRs[i,6]+post3HRs[i,7])*
       (post3FARs[i,6]-post3FARs[i,7])) +
    
    (.5 * (post3HRs[i,7]+0)*
       (post3FARs[i,7]-0)) 
  
}
posttest3.AUC = auc

# Calculate the mean and sd of AUC values
posttest3.mean.AUC = mean(posttest3.AUC)
posttest3.sd.AUC = sd(posttest3.AUC)

posttest3.mean.AUC
posttest3.sd.AUC

# Calculate HRs and FARs for each pid at each criterion with a loglinear correction
for (i in 1:length(unique(PostShare$pid))) { 
  current_pid <- unique(PostShare$pid)[i]
  
  h1 = subset(PostShare$share_, PostShare$pid == current_pid & PostShare$headline_type == 'true' 
              & PostShare$share_ >= 1)
  HR1[i] = length(h1)/8
  
  fa1 = subset(PostShare$share_, PostShare$pid == current_pid & PostShare$headline_type == 'false' 
               & PostShare$share_ >= 1)
  FAR1[i] = length(fa1)/8
  
  h2 = subset(PostShare$share_, PostShare$pid == current_pid & PostShare$headline_type == 'true' 
              & PostShare$share_ >= 2)
  HR2[i] = (length(h2)+0.5)/(8+1)
  
  fa2 = subset(PostShare$share_, PostShare$pid == current_pid & PostShare$headline_type == 'false' 
               & PostShare$share_ >= 2)
  FAR2[i] = (length(fa2)+0.5)/(8+1)
  
  h3 = subset(PostShare$share_, PostShare$pid == current_pid & PostShare$headline_type == 'true' 
              & PostShare$share_ >= 3)
  HR3[i] = (length(h3)+0.5)/(8+1)
  
  fa3 = subset(PostShare$share_, PostShare$pid == current_pid & PostShare$headline_type == 'false' 
               & PostShare$share_ >= 3)
  FAR3[i] = (length(fa3)+0.5)/(8+1)
  
  h4 = subset(PostShare$share_, PostShare$pid == current_pid & PostShare$headline_type == 'true' 
              & PostShare$share_ >= 4)
  HR4[i] = (length(h4)+0.5)/(8+1)
  
  fa4 = subset(PostShare$share_, PostShare$pid == current_pid & PostShare$headline_type == 'false' 
               & PostShare$share_ >= 4)
  FAR4[i] = (length(fa4)+0.5)/(8+1)
  
  h5 = subset(PostShare$share_, PostShare$pid == current_pid & PostShare$headline_type == 'true' 
              & PostShare$share_ >= 5)
  HR5[i] = (length(h5)+0.5)/(8+1)
  
  fa5 = subset(PostShare$share_, PostShare$pid == current_pid & PostShare$headline_type == 'false' 
               & PostShare$share_ >= 5)
  FAR5[i] = (length(fa5)+0.5)/(8+1)
  
  h6 = subset(PostShare$share_, PostShare$pid == current_pid & PostShare$headline_type == 'true' 
              & PostShare$share_ >= 6)
  HR6[i] = (length(h6)+0.5)/(8+1)
  
  fa6 = subset(PostShare$share_, PostShare$pid == current_pid & PostShare$headline_type == 'false' 
               & PostShare$share_ >= 6)
  FAR6[i] = (length(fa6)+0.5)/(8+1)
  
  h7 = subset(PostShare$share_, PostShare$pid == current_pid & PostShare$headline_type == 'true' 
              & PostShare$share_ >= 7)
  HR7[i] = (length(h7)+0.5)/(8+1)
  
  fa7 = subset(PostShare$share_, PostShare$pid == current_pid & PostShare$headline_type == 'false' 
               & PostShare$share_ >= 7)
  FAR7[i] = (length(fa7)+0.5)/(8+1)
  
}

# Combine the HRs for each different criterion into a single data frame, do the same for FARs
HRs6 = data.frame(HR1, HR2, HR3, HR4, HR5, HR6, HR7) 
FARs6 = data.frame(FAR1, FAR2, FAR3, FAR4, FAR5, FAR6, FAR7)

# Calculate B"D values
BD52 = 0
BD53 = 0 
BD54 = 0
BD55 = 0
BD56 = 0
BD57 = 0

for (i in 1:(length(unique(PostShare$pid)))) {
  BD52[i] = ( ((1 - HRs6[i,2]) * (1 - FARs6[i,2])) - (HRs6[i,2]*FARs6[i,2]) ) / ( ((1 - HRs6[i,2]) * (1 - FARs6[i,2])) + (HRs6[i,2]*FARs6[i,2]) )
  BD53[i] = ( ((1 - HRs6[i,3]) * (1 - FARs6[i,3])) - (HRs6[i,3]*FARs6[i,3]) ) / ( ((1 - HRs6[i,3]) * (1 - FARs6[i,3])) + (HRs6[i,3]*FARs6[i,3]) )
  BD54[i] = ( ((1 - HRs6[i,4]) * (1 - FARs6[i,4])) - (HRs6[i,4]*FARs6[i,4]) ) / ( ((1 - HRs6[i,4]) * (1 - FARs6[i,4])) + (HRs6[i,4]*FARs6[i,4]) )
  BD55[i] = ( ((1 - HRs6[i,5]) * (1 - FARs6[i,5])) - (HRs6[i,5]*FARs6[i,5]) ) / ( ((1 - HRs6[i,5]) * (1 - FARs6[i,5])) + (HRs6[i,5]*FARs6[i,5]) )
  BD56[i] = ( ((1 - HRs6[i,6]) * (1 - FARs6[i,6])) - (HRs6[i,6]*FARs6[i,6]) ) / ( ((1 - HRs6[i,6]) * (1 - FARs6[i,6])) + (HRs6[i,6]*FARs6[i,6]) )
  BD57[i] = ( ((1 - HRs6[i,7]) * (1 - FARs6[i,7])) - (HRs6[i,7]*FARs6[i,7]) ) / ( ((1 - HRs6[i,7]) * (1 - FARs6[i,7])) + (HRs6[i,7]*FARs6[i,7]) )
}
posttest3.BD = cbind(BD52, BD53, BD54, BD55, BD56, BD57)

# Calculate the mean B"D value for each pid
posttest3.BD.means = rowMeans(posttest3.BD)

# Calculate the mean and sd of B"D values at each scale point apart from 1 
posttest3.BD.scale.means = colMeans(posttest3.BD)
posttest3.BD.scale.sds = apply(posttest3.BD,2,sd)

posttest3.BD.scale.means
posttest3.BD.scale.sds

# Calculate the mean and sd of B"D values across all scale points apart from 1
posttest3.mean.BD = mean(c(posttest3.BD))
posttest3.sd.BD = sd(c(posttest3.BD))

posttest3.mean.BD
posttest3.sd.BD


```

## ROC- Sharing Intention #####

```{r, fig.width=20, fig.height=8}
# Plot ROCs
par(mfrow = c(1, 2))
par(mar=c(4,4,4,4))

plot(pretest3.FAR.means, pretest3.HR.means, ylim=c(0,1), xlim=c(0,1), yaxs="i", xaxs="i", 
     cex.lab = 1.2, cex.axis = 1.2, cex = 1.2, cex.main = 1.4,
     xlab = "False Alarm Rate", ylab = "Hit Rate", main = "ROC plot for the Control Condition-Sharing Intention",
     pch = 19, col = "red")
abline(coef = c(0,1))
lines(pretest3.FAR.means, pretest3.HR.means, col = "red") # ROC curve for pretest
points(posttest3.FAR.means, posttest3.HR.means, pch = 15, col = "blue", cex = 1.2)
lines(posttest3.FAR.means, posttest3.HR.means, col = "blue") # ROC curve for posttest
legend( x="bottomright", 
        legend=c("Pretest", "Posttest"),
        col=c("red","blue"), pch = c(19,15), 
        pt.cex = c(1.2,1.2), cex = 1.2, lwd=1, lty=c(1))
```

```{r}

## AUC Analysis ##

# Conduct t-test comparing AUC values at each test
t.test(posttest3.AUC, pretest3.AUC, paired= T)

# Calculate effect size
cohen.d(posttest3.AUC, pretest3.AUC, paired= T)

# Calculate Bayes factor
ttestBF(posttest3.AUC, pretest3.AUC, paired= T)

```
#sensitivity analysis
```{r}
t.test(posttest3.BD.means, pretest3.BD.means, paired= T)

# Calculate effect size
cohen.d(posttest3.BD.means, pretest3.BD.means, paired= T)

# Calculate Bayes factor
ttestBF(posttest3.BD.means, pretest3.BD.means, paired= T)
```
```{r, fig.width=20, fig.height=4}
# Plot ROCs
par(mfrow = c(1, 3))
par(mar=c(4,4,4,4))

p1<- plot(pretest1.FAR.means, pretest1.HR.means, ylim=c(0,1), xlim=c(0,1), yaxs="i", xaxs="i", 
     cex.lab = 1.2, cex.axis = 1.2, cex = 1.2, cex.main = 1.4,
     xlab = "False Alarm Rate", ylab = "Hit Rate", main = "ROC plot for the Control Condition-Reliability",
     pch = 19, col = "red")
abline(coef = c(0,1))
lines(pretest1.FAR.means, pretest1.HR.means, col = "red") # ROC curve for pretest
points(posttest1.FAR.means, posttest1.HR.means, pch = 15, col = "blue", cex = 1.2)
lines(posttest1.FAR.means, posttest1.HR.means, col = "blue") # ROC curve for posttest
legend( x="bottomright", 
        legend=c("Pretest", "Posttest"),
        col=c("red","blue"), pch = c(19,15), 
        pt.cex = c(1.2,1.2), cex = 1.2, lwd=1, lty=c(1))

# Plot ROCs
par(mar=c(4,4,4,4))

p3<- plot(pretest3.FAR.means, pretest3.HR.means, ylim=c(0,1), xlim=c(0,1), yaxs="i", xaxs="i", 
     cex.lab = 1.2, cex.axis = 1.2, cex = 1.2, cex.main = 1.4,
     xlab = "False Alarm Rate", ylab = "Hit Rate", main = "ROC plot for the Control Condition-Sharing Intention",
     pch = 19, col = "red")
abline(coef = c(0,1))
lines(pretest3.FAR.means, pretest3.HR.means, col = "red") # ROC curve for pretest
points(posttest3.FAR.means, posttest3.HR.means, pch = 15, col = "blue", cex = 1.2)
lines(posttest3.FAR.means, posttest3.HR.means, col = "blue") # ROC curve for posttest
legend( x="bottomright", 
        legend=c("Pretest", "Posttest"),
        col=c("red","blue"), pch = c(19,15), 
        pt.cex = c(1.2,1.2), cex = 1.2, lwd=1, lty=c(1))

# Plot ROCs
par(mar=c(4,4,4,4))

p2<- plot(pretest2.FAR.means, pretest2.HR.means, ylim=c(0,1), xlim=c(0,1), yaxs="i", xaxs="i", 
     cex.lab = 1.2, cex.axis = 1.2, cex = 1.2, cex.main = 1.4,
     xlab = "False Alarm Rate", ylab = "Hit Rate", main = "ROC plot for the Control Condition-Confidence in Judgment",
     pch = 19, col = "red")
abline(coef = c(0,1))
lines(pretest2.FAR.means, pretest2.HR.means, col = "red") # ROC curve for pretest
points(posttest2.FAR.means, posttest2.HR.means, pch = 15, col = "blue", cex = 1.2)
lines(posttest2.FAR.means, posttest2.HR.means, col = "blue") # ROC curve for posttest
legend( x="bottomright", 
        legend=c("Pretest", "Posttest"),
        col=c("red","blue"), pch = c(19,15), 
        pt.cex = c(1.2,1.2), cex = 1.2, lwd=1, lty=c(1))


```
